<template>
  <div class="login-container">

    <div class="login-form">
      <div class="log-img">
        <img src="../../assets/logo.png" height="65px" alt="">
      </div>

      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="form-vw" auto-complete="on" label-position="left">
        <div class="title-container">
          <h2 class="title">登录</h2>
        </div>

        <el-form-item prop="username">
          <el-input
            ref="username"
            v-model="un"
            size="medium"
            placeholder="账号"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            ref="password"
            v-model="pw"
            size="medium"
            show-password
            type="password"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
        </el-form-item>

        <div class="one-vw">
          <div>
            <el-checkbox v-model="checked">记住密码</el-checkbox>
          </div>

          <div hidden>
            <el-button type="text" @click="toForget">忘记密码？</el-button>
          </div>
        </div>

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>

      </el-form>
    </div>

    <el-dialog
      title="忘记密码"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      center
      top="25vh"
      width="500px"
    >
      <div>
        <el-form
          ref="ruleForm"
          class="forget-form"
          :model="ruleForm"
          :rules="rules"
          label-width="110px"
        >
          <el-form-item label="身份证号码:" prop="cardid">
            <el-input v-model="ruleForm.cardid" clearable placeholder="请输入身份证号码"/>
          </el-form-item>
          <el-form-item label="新密码:" prop="newpassword">
            <el-input v-model="ruleForm.newpassword" type="password" clearable placeholder="请输入新密码"/>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import axios from "axios";
  import G from "@/components/G";

  export default {
    name: 'Login',
    data() {
      const validateUsername = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'))
        } else {
          callback()
        }
      }
      const validatePassword = (rule, value, callback) => {
        if (value.length < 1) {
          callback(new Error('请输入密码'))
        } else {
          callback()
        }
      }
      return {
        un: '',
        pw: '',
        loginForm: {
          username: 'admin',
          password: '111111'
        },
        loginRules: {
          username: [{required: true, trigger: 'blur', validator: validateUsername}],
          password: [{required: true, trigger: 'blur', validator: validatePassword}]
        },
        checked: false,
        loading: false,
        redirect: undefined,

        dialogVisible: false,
        ruleForm: {
          cardid: '',
          newpassword: ''
        },
        rules: {
          cardid: [{required: true, trigger: 'blur', message: '请输入'}],
          newpassword: [
            {required: true, message: '请输入新密码', trigger: 'blur'}
          ]
        }
      }
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    methods: {
      handleLogin() {
        let param = '&username=' + this.un
          + '&password=' + this.pw
        axios.post(G.host + 'login', param).then(res => {
          this.$notify({
            title: '登录',
            message: this.$createElement('i', {style: 'color: teal'}, res.data.message),
            duration: 2000
          })
          if (res.data.code === 0) {
            this.$refs.loginForm.validate(valid => {
              if (valid) {
                this.loading = true
                this.$store.dispatch('user/login', this.loginForm).then(() => {
                  this.$router.push({path: this.redirect || '/list'})
                  this.loading = false
                  G.username = this.un
                }).catch(() => {
                  this.loading = false
                })
              } else {
                console.log('error submit!!')
                return false
              }
            })
          }
        })


      },
      toForget() {
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs.ruleForm.resetFields()
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            /* const params = JSON.parse(JSON.stringify(this.ruleForm))
            params.newpassword = md5(this.ruleForm.newpassword)
            resetpwd(params).then(res => {
              this.$message.success(res.msg)
              this.dialogVisible = false
            }) */
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
        this.dialogVisible = false
      }
    }
  }
</script>

<style>
  .el-button--primary {
    background-color: #007bfc;
    border-color: #007bfc;
  }

  .el-checkbox__input.is-checked + .el-checkbox__label, .el-button--text, .el-button--text:hover {
    color: #2f54ea;
  }

  .el-checkbox__input.is-checked .el-checkbox__inner {
    background-color: #2f54ea;
    border-color: #2f54ea;
  }
</style>

<style lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;

  .login-container {
    min-height: 100%;
    width: 100%;
    overflow: hidden;
    background-image: url(../../assets/log_bj.jpg);
    background-size: cover;
    background-repeat: no-repeat;

    .login-form {
      position: absolute;
      left: 50%;
      top: 50%;
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
      width: 400px;
      max-width: 100%;
      overflow: hidden;

      .form-vw {
        background-color: #fff;
        padding: 60px 40px;
        border-radius: 4px;
      }

      .log-img {
        text-align: center;
        margin-bottom: 10px;
      }
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        color: #000;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: normal;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .one-vw {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 22px;
    }

  }
</style>
